import mb4_file
import flet as ft
import SHA2_RSA2048_file

model=0
def main(page: ft.Page):
    

    global model
        
        #加密
    def SM4_cbc_enc(e):
        #窗口实现
        Key=mb4_file.get_key(key.value)
        Iv=mb4_file.get_iv(key.value)
        plaintext_bytes=mb4_file.exp_file(fileinPath.value)
        key_bytes=bytes(Key,encoding="utf8")
        iv_bytes=bytes(Iv,encoding="utf8")
        ciphertext_bytes=mb4_file.SM4_cbc_enc(plaintext_bytes,iv_bytes,key_bytes)
        with open(fileoutPath.value,'wb')as f:
            f.write(ciphertext_bytes)
        f.close

            # 弹窗
        dlg = ft.AlertDialog(title=ft.Text("加密成功!"))
        page.dialog=dlg
        dlg.open=True
        page.update

        Sm4_clean_text(e)

        #解密
    def SM4_cbc_dec(e):
        #窗口实现
        Key=mb4_file.get_key(key.value)
        Iv=mb4_file.get_iv(key.value)
        key_bytes=bytes(Key,encoding="utf8")
        iv_bytes=bytes(Iv,encoding="utf8")
        ciphertext_bytes=mb4_file.exp_file(fileinPath.value)
        plaintext_bytes=mb4_file.SM4_cbc_dec(ciphertext_bytes,iv_bytes,key_bytes)
        with open(fileoutPath.value,'wb')as f:
            #f.truncate
            f.write(plaintext_bytes)
        f.close

            # 弹窗
        dlg = ft.AlertDialog(title=ft.Text("解密成功!"))
        page.dialog=dlg
        dlg.open=True
        page.update

        Sm4_clean_text(e)
            
    def Sm4_clean_text(e):
        key.value=""
        fileinPath.value=""
        fileoutPath.value=""
        page.update()

    def Sm4_main(e):
        global model
        if(model==0):    
            page.add(
                    key,
                    fileinPath,
                    # pickButton,
                    fileoutPath,
                    enc,
                    dec,
                    greetings,
                )
            model=1
        elif(model==2):
            page.remove(
                title01,
                Pub_key_Path,
                Pri_key_Path,
                CreateKey,
                title02,
                Pri_Key_Path,
                FileinPath,
                FileoutPath,
                CreateSign,
                title03,
                Pub_Key_Path,
                fileInPath,
                SignPath,
                Rsa_verify,
                greetings,
            )
            page.add(
                key,
                fileinPath,
                fileoutPath,
                enc,
                dec,
                greetings,
            )
            model=1

    def pick_files_result(e: ft.FilePickerResultEvent):
        fileinPath.value=e.path
        fileinPath.update()



    
    key=ft.TextField(label="key",autofocus=True,password=True,width=800)
    fileinPath=ft.TextField(label="fileInPath",width=800)
    fileoutPath=ft.TextField(label="fileOutPath",width=800)
    enc=ft.ElevatedButton(text="加密",on_click=SM4_cbc_enc,icon=ft.icons.TASK_OUTLINED,width=300)
    dec=ft.ElevatedButton(text="解密",on_click=SM4_cbc_dec,icon=ft.icons.TASK_ROUNDED,width=300)
    greetings=ft.Column()
    


    def createKey(e):
        SHA2_RSA2048_file.gen_rsa_key(Pri_key_Path.value,Pub_key_Path.value)
        dlg = ft.AlertDialog(title=ft.Text("成功!"))
        page.dialog=dlg
        dlg.open=True
        page.update()

    def createSign(e):
        private_key=SHA2_RSA2048_file.exp_file(Pri_Key_Path.value)
        plaintext=SHA2_RSA2048_file.exp_file(FileinPath.value)
        signature=SHA2_RSA2048_file.rsa_sign(plaintext,private_key)
        with open(FileoutPath.value,"wb") as f:
            f.write(signature)
        f.close()
        dlg = ft.AlertDialog(title=ft.Text("成功!"))
        page.dialog=dlg
        dlg.open=True
        page.update()

    def rsa_verify(e):
        public_key=SHA2_RSA2048_file.exp_file(Pub_Key_Path.value)
        plaintext=SHA2_RSA2048_file.exp_file(fileInPath.value)
        signature=SHA2_RSA2048_file.exp_file(SignPath.value)
        result=SHA2_RSA2048_file.rsa_verify(signature,plaintext,public_key)
        if(result):
            dlg = ft.AlertDialog(title=ft.Text("验证通过!"))
            page.dialog=dlg
            dlg.open=True
            page.update()
        else:
            dlg = ft.AlertDialog(title=ft.Text("未通过!"))
            page.dialog=dlg
            dlg.open=True
            page.update()    


    title01=ft.Text("RSA生成证书模块", style=ft.TextThemeStyle.TITLE_MEDIUM)
    Pub_key_Path=ft.TextField(label="publickeyPath",width=800)
    Pri_key_Path=ft.TextField(label="privatekeyPath",width=800)
    CreateKey=ft.ElevatedButton(text="生成",on_click=createKey,width=300,icon=ft.icons.ADD_TASK_OUTLINED)

    title02=ft.Text("私钥创建证书",style=ft.TextThemeStyle.TITLE_MEDIUM)
    Pri_Key_Path=ft.TextField(label="privatekeyPath",width=800)
    FileinPath=ft.TextField(label="fileInPath",width=800)
    FileoutPath=ft.TextField(label="fileOutPath",width=800)
    CreateSign=ft.ElevatedButton(text="创建",on_click=createSign,width=300,icon=ft.icons.ADD_TASK_OUTLINED)

    title03=ft.Text("公钥验证证书",style=ft.TextThemeStyle.TITLE_MEDIUM)
    Pub_Key_Path=ft.TextField(label="publickeyPath",width=800)
    fileInPath=ft.TextField(label="fileInPath",width=800)
    SignPath=ft.TextField(label="signPath",width=800)
    Rsa_verify=ft.ElevatedButton(text="验证",on_click=rsa_verify,width=300,icon=ft.icons.ADD_TASK_OUTLINED)

    def RSA2048_main(e):
        global model
        if(model==0):
            page.add(
                title01,
                Pub_key_Path,
                Pri_key_Path,
                CreateKey,
                title02,
                Pri_Key_Path,
                FileinPath,
                FileoutPath,
                CreateSign,
                title03,
                Pub_Key_Path,
                fileInPath,
                SignPath,
                Rsa_verify,
                greetings,
            )
            model=2
        elif(model==1):
            page.remove(
                key,
                fileinPath,
                fileoutPath,
                enc,
                dec,
                greetings,
            )
            page.add(
                title01,
                Pub_key_Path,
                Pri_key_Path,
                CreateKey,
                title02,
                Pri_Key_Path,
                FileinPath,
                FileoutPath,
                CreateSign,
                title03,
                Pub_Key_Path,
                fileInPath,
                SignPath,
                Rsa_verify,
                greetings,
            )
            model=2


    x=ft.AppBar(
        title=ft.Text("加密系统"),
        center_title=False,
        bgcolor=ft.colors.BLUE_400,
        actions=[
            ft.PopupMenuButton(
                icon=ft.icons.SETTINGS
                ,items=[
                    ft.PopupMenuItem(text="SM4加解密文件",on_click=Sm4_main),
                    ft.PopupMenuItem(text="RSA2048证书验证",on_click=RSA2048_main),
                ]
            ),
        ],
    )

    # scrollmodel=ft.ScrollMode.HIDDEN
    # print(scrollmodel)
    page.scroll=ft.ScrollMode.HIDDEN


    # pick_files_dialog = ft.FilePicker(on_result=pick_files_result)

    # pickButton=ft.ElevatedButton(
    #                 "Pick files",
    #                 icon=ft.icons.UPLOAD_FILE,
    #                 on_click=lambda _: pick_files_dialog.pick_files(
    #                     allow_multiple=True
    #                 )
    #     )
    # container = ft.Container(theme=ft.Theme(color_scheme=ft.ColorScheme(primary=ft.colors.YELLOW)))
    
    page.theme = ft.Theme(color_scheme_seed='Indigo')
    page.add(
        x,
    )
    
ft.app(target=main)